package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());


        //判断当前加入到购物车的商品是否存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在，只需数量加1
        if (list!=null && list.size()>0){
            ShoppingCart cart=list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
            return;
        }
        //不存在，直接插入
        //判断添加的商品是菜品还是套餐
        Long dishId = shoppingCartDTO.getDishId();
        //如果是菜品
        if (dishId!=null){
            Dish dish = dishMapper.getById(dishId);
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());

        }
        else{
            //如果是套餐
            Long setmealId = shoppingCartDTO.getSetmealId();
            shoppingCart.setName(setmealMapper.getById(setmealId).getName());
            shoppingCart.setImage(setmealMapper.getById(setmealId).getImage());
            shoppingCart.setAmount(setmealMapper.getById(setmealId).getPrice());

        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public Object list() {
        List<ShoppingCart> list = shoppingCartMapper.list(new ShoppingCart());
        return list;
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //如果商品数量大于1，数量减一，否则删除
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list!=null && list.size()>0){
            ShoppingCart cart=list.get(0);
            cart.setNumber(cart.getNumber()-1);
            if (cart.getNumber()==0){
                shoppingCartMapper.deleteById(cart.getId());
            }else {
                shoppingCartMapper.updateNumberById(cart);
            }
        }
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

}
